package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmProcess;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmTask;
import com.harmontronics.hanma.paas.paas.domain.app.entity.UserTaskForm;
import com.harmontronics.hanma.paas.paas.dto.input.process.CompleteTaskRequest;
import com.harmontronics.hanma.paas.paas.dto.input.process.ProcessStartRequest;
import com.harmontronics.hanma.paas.paas.dto.output.HmProcessResult;
import com.harmontronics.hanma.paas.paas.service.HmProcessRuntimeAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 吕大兵
 */
@RestController
@RequestMapping("/api/hm-process-runtime")
@Api(tags = {"流程运行时接口"})
public class HmProcessRuntimeController {

    @Autowired
    private HmProcessRuntimeAppService hmProcessRuntimeAppService;


    /**
     * 启动一个流程
     *
     * @param startRequest
     * @return
     */
    @PostMapping("start")
    @ApiOperation("启动流程")
    public ResponseEntity<String> startProcess(@RequestBody ProcessStartRequest startRequest) {
        hmProcessRuntimeAppService.start(startRequest.getProcessId(), startRequest.getValues(), startRequest.toCarbonCopyVo());
        return ResponseEntity.ok();
    }


    @GetMapping("task-form-key")
    @ApiOperation("获取用户任务节点表单ID")
    public ResponseEntity<UserTaskForm> getTaskFormId(@RequestParam String taskId) {
        UserTaskForm form = hmProcessRuntimeAppService.getTaskFormId(taskId);
        return ResponseEntity.ok(form);
    }


    @PutMapping(value = "process/operation")
    @ApiOperation("完成任务")
    public ResponseEntity<String> operateProcess(@RequestBody CompleteTaskRequest taskRequest) throws SQLException, IOException {
        hmProcessRuntimeAppService.completeTask(taskRequest.getProcessId(), taskRequest.getProcessInstanceId(), taskRequest.getTaskId(), taskRequest.getMessage(), taskRequest.getValues(), taskRequest.toCarbonCopyVo(), taskRequest.getModelKey(), taskRequest.getServiceKey());
        return ResponseEntity.ok();
    }


    @GetMapping(value = "task/todo")
    @ApiOperation(value = "需要去处理的任务")
    public ResponseEntity<PageBase<HmTask>> listToDo(
            @ApiParam("流程ID") @RequestParam(required = false) Long processId,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTask> pageBase = hmProcessRuntimeAppService.listToDo(processId, pageNo, pageSize);
        return ResponseEntity.ok(pageBase);
    }

    @GetMapping(value = "process/ongoing")
    @ApiOperation(value = "获取正在进行中的流程")
    public ResponseEntity<List<HmProcessResult>> getOngoingProcess() {
        List<HmProcess> processes = hmProcessRuntimeAppService.getOnGoingProcess();
        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, processes));
    }

    @GetMapping(value = "process/my-start")
    @ApiOperation(value = "我发起的请求")
    public ResponseEntity<List<HmProcessResult>> getMyStartProcess() {
        List<HmProcess> processes = hmProcessRuntimeAppService.getMyStartProcess();
        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, processes));
    }

    @GetMapping(value = "process/my-start-todo")
    @ApiOperation(value = "我发起的处理中的流程任务")
    public ResponseEntity<PageBase<HmTask>> getMyStartProcessToDo(@ApiParam("流程ID") @RequestParam(required = false) Long processId,
                                                                  @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                                  @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTask> pageBase = hmProcessRuntimeAppService.listMyStartProcessToDo(processId, pageNo, pageSize);
        return ResponseEntity.ok(pageBase);
    }

    @GetMapping(value = "process/my-start-finish")
    @ApiOperation(value = "我发起的完成的流程任务")
    public ResponseEntity<PageBase<HmTask>> getMyStartProcessFinish(@ApiParam("流程ID") @RequestParam(required = false) Long processId,
                                                                    @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                                    @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTask> pageBase = hmProcessRuntimeAppService.listMyStartProcessFinish(processId, pageNo, pageSize);
        return ResponseEntity.ok(pageBase);
    }
}
